package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	public class Title extends MovieClip
	{
		private var _mc:MovieClip;
		
		private var _func:Function;
		
		public function Title(mc:MovieClip, func:Function)
		{
			_mc = mc;
			_func = func;
			_mc.bg.addEventListener(MouseEvent.CLICK, onClear);
			addEvent(_mc);
		}
		
		private function onClear(e:MouseEvent):void
		{
			clear(_mc);
		}
		
		private function onSelect(e:MouseEvent):void
		{
			var sbtn:SimpleButton = e.currentTarget as SimpleButton;
			setSelect(sbtn);
		}
		
		private function addEvent(t:MovieClip):void
		{
			for(var i:int = 0;i< t.numChildren;i++)
			{
				if(t.hasOwnProperty("btn_" + (i + 1)))
				{
					var btn:SimpleButton = t["btn_" + (i + 1)];
					btn.addEventListener(MouseEvent.CLICK, onSelect);
					var mv:MovieClip = t["btn_" + (i + 1) + "_0"];
					if(mv)
					{
						mv.visible = false;
						addEvent(mv);
					}
				}
				else if(t.hasOwnProperty("target"))
				{
					var btn2:SimpleButton = t["target"];
					btn2.addEventListener(MouseEvent.CLICK, onTarget);
				}
				else
				{
					break;
				}
			}
		}
		
		private function onTarget(e:MouseEvent):void
		{
			trace(e.currentTarget);
			if(_func != null)
			{
				_func();
			}
			else
			{
				trace("_func is null!!!!!!!!!!!!!!");
			}
			var sbtn:SimpleButton = e.currentTarget as SimpleButton;
			setSelect(sbtn);
		}
		
		private function setSelect(sbtn:SimpleButton):void
		{
			clear(sbtn.parent as MovieClip);
			if(sbtn.parent.hasOwnProperty(sbtn.name + "_0"))
			{
				var mv:MovieClip = sbtn.parent[sbtn.name + "_0"];
				clear(mv);
				mv.visible = true;
			}
			else
			{
				clear(_mc);
			}
		}
		
		private function clear(t:MovieClip):void
		{
			for(var i:int = 0;i< t.numChildren;i++)
			{
				if(t.hasOwnProperty("btn_" + (i + 1) + "_0")) 
				{
					var mv:MovieClip = t["btn_" + (i + 1) + "_0"];
					mv.visible = false;
					clear(mv);
				}
			}
		}
	}
}